package com.captjack.wechat.cp;

import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.bean.WxCpMessage;
import me.chanjar.weixin.cp.message.WxCpMessageRouter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author Capt Jack
 * @date 2018/5/25
 */
@Component
public class HelloController {

    private final WxCpService wxCpService;

    private final WxCpMessageRouter wxCpMessageRouter;

    public String hello(String id) {
        try {
            /*
            wechat:
              cp:
                corpId: 组织id
                agentId: 24
                secret: 加密串
                token: token
                aesKey: 消息加密key
            */
            System.out.println(wxCpService.getUserService().getById("18142317296").toString());
            System.out.println(wxCpService.getAccessToken());
            WxCpMessage wxCpMessage = new WxCpMessage();
            // 发送消息
            wxCpMessage.setAgentId(0);
            wxCpMessage.setContent("测试消息");
            wxCpMessage.setMsgType("text");
            wxCpMessage.setToUser("18142317296");
            System.out.println(wxCpService.messageSend(wxCpMessage).toString());
        } catch (WxErrorException e) {
            e.printStackTrace();
        }
        return "";
    }

    @Autowired
    public HelloController(WxCpService wxCpService, WxCpMessageRouter wxCpMessageRouter) {
        this.wxCpService = wxCpService;
        this.wxCpMessageRouter = wxCpMessageRouter;
    }

}
